﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>网页计数器</title>
</head>
<body>
    <button onclick="startCount()">开始计数!</button>
    <input type="text" id="txt" />
    <button onclick="stopCount()">
        停止计数!
    </button>

    <script>
        let c = 0;
        let t;
        let timer_is_on = 0;
        function timedCount() {
            document.getElementById("txt").value = c;
            c = c + 1;
            t = setTimeout(function () {
                timedCount
            }, 1000);
        }

        function startCount() {
            if (!timer_is_on) {
                timer_is_on = 1;
                timedCount();
            }
        }

        function stopCount() {
            clearTimeout(t);
            timer_is_on = 0;
        }
    </script>
</body>
</html>